#!/bin/bash

set -e -o pipefail

BASE=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")

if [ "$(uname -s)" = "Darwin" ]; then
	DISTRIB=macOS
else
	DISTRIB=$(cat /etc/lsb-release | grep "DISTRIB_ID" | awk -F "=" '{print $2}')
	DISTRIB_RELEASE=$(cat /etc/lsb-release | grep "DISTRIB_RELEASE" | awk -F "=" '{print $2}')
fi

KHADAS_TOOL_NAME="aml-burn-tool"
INSTALL_DIR="/usr/local/bin"

RULE=

RED='\033[0;31m'
RESET='\033[m'

error_msg() {
	echo -e ${RED}Error:${RESET} $1
}

echo ""
echo "==============================================="
echo ""
echo "Host PC: $DISTRIB $DISTRIB_RELEASE"
echo ""
echo "==============================================="
echo ""


if [[ "$DISTRIB" != "macOS" ]]; then
	echo "Removing USB rules..."

	if [[ "$DISTRIB_RELEASE" =~ "12" ]]; then
		RULE="/etc/udev/rules.d/70-persistent-usb-ubuntu12.rules"
	elif [[ "$DISTRIB_RELEASE" =~ "14" || "$DISTRIB_RELEASE" =~ "16" || "$DISTRIB_RELEASE" =~ "18" || "$DISTRIB_RELEASE" =~ "20" ]]; then
		RULE="/etc/udev/rules.d/70-persistent-usb-ubuntu14.rules"
	else
	#	error_msg "Ubuntu $DISTRIB_RELEASE haven't been verified!"
	#	exit 1
		RULE="/etc/udev/rules.d/70-persistent-usb-ubuntu14.rules"
	fi

	sudo rm -f $RULE
	sudo udevadm control --reload-rules
fi

echo "Removing flash-tool..."
sudo rm -f $INSTALL_DIR/$KHADAS_TOOL_NAME

echo "Done!"
